package tools.aligning;

import java.util.ArrayList;
import java.util.Collections;

public class LocalMutationList extends ArrayList<LocalMutation> {


	private static final long serialVersionUID = 1L;
	private boolean sorted;

	public LocalMutationList(LocalMutationList lml){
		this();
		for(LocalMutation lm : lml){
			this.add(new LocalMutation(lm));
		}
	}
	
	public LocalMutationList(){
		super();
		sorted=false;
	}
	
	public LocalMutationList(String s){
		this();
		String[] l= s.split(",");
		for (String string : l) {
			add(new LocalMutation(string));
		}
	}
	
	public String mutationListEMS(){
		if(this.size()==1){
			return this.get(0).EMS();
		}
		StringBuffer sb= new StringBuffer();
		for(LocalMutation lm : this){
			sb.append(lm.EMS());
		}
		return sb.toString();
	}
	
	public void shift(int refPoint){
		for ( LocalMutation lm : this) {
			lm.shift(refPoint);
		}
	}
	
	public void shiftRevComp(int refPoint){
		for (LocalMutation lm : this) {
			lm.shiftRevComp(refPoint);
		}
		sorted=false;
	}
	
	public boolean encapsulated(int start, int end){
		for( LocalMutation lm : this) {
			if(lm.encapsulated(start, end)){
				return true;
			}
		}
		return false;
	}
	
	public void sort(){
		Collections.sort(this);
		sorted=true;
	}
	
	public String toString(){
		if(!sorted){
			sort();
		}
		if(size()==0){
			return "";
		}
		StringBuffer out= new StringBuffer(get(0).toString());
		for(int i=1;i<size();++i){
			out.append(",");
			out.append(get(i).toString());
		}
		return out.toString();
	}
}
